package com.tadpole.music.presenter.me.yun;

import com.tadpole.music.bean.me.yun.YunSubjectBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.me.yun.YunSubjectIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取 蝌蚪云考试考题
 * Created By 孙岩
 * on 2021/2/9
 */
public class YunSubjectPresenter extends BasePresenter<YunSubjectIView> {

    @SuppressWarnings("unchecked")
    public void getYunSubject() {
        if (isViewAttached()) {
            String url = UrlConfig.getYunSubject;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.YUN_TOKEN, "");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    List<YunSubjectBean.DataBeanX.DataBean> list = new ArrayList<>();
                                    JSONArray array = new JSONArray(object.getString("data"));
                                    for (int i = 0; i < array.length(); i++) {
                                        JSONObject jsonObject = new JSONObject(array.get(i).toString());
                                        YunSubjectBean.DataBeanX.DataBean dataBean = new YunSubjectBean.DataBeanX.DataBean(
                                                JsonParseUtil.getInt(jsonObject, "id"),
                                                JsonParseUtil.getStr(jsonObject, "created_at"),
                                                JsonParseUtil.getStr(jsonObject, "updated_at"),
                                                JsonParseUtil.getStr(jsonObject, "title"),
                                                JsonParseUtil.getInt(jsonObject, "college_id"),
                                                JsonParseUtil.getInt(jsonObject, "question_rank_id"),
                                                JsonParseUtil.getInt(jsonObject, "question_class_id"),
                                                JsonParseUtil.getInt(jsonObject, "question_type_id"),
                                                JsonParseUtil.getInt(jsonObject, "status"),
                                                JsonParseUtil.getInt(jsonObject, "play_times"),
                                                JsonParseUtil.getInt(jsonObject, "interval"),
                                                JsonParseUtil.getInt(jsonObject, "answer_type"),
                                                JsonParseUtil.getStr(jsonObject, "answer_a"),
                                                JsonParseUtil.getStr(jsonObject, "answer_b"),
                                                JsonParseUtil.getInt(jsonObject, "right_answer"),
                                                JsonParseUtil.getStr(jsonObject, "comment"),
                                                JsonParseUtil.getStr(jsonObject, "title_img"),
                                                JsonParseUtil.getStr(jsonObject, "title_sound"),
                                                JsonParseUtil.getStr(jsonObject, "answer_c"),
                                                JsonParseUtil.getStr(jsonObject, "answer_d"),
                                                JsonParseUtil.getStr(jsonObject, "bg_sound")
                                        );
                                        list.add(dataBean);
                                    }
                                    YunSubjectBean.DataBeanX bean = new YunSubjectBean.DataBeanX(
                                            JsonParseUtil.getInt(object, "exam_id"),
                                            list
                                    );
                                    getView().showSubject(bean);
                                } else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
